<template>
  <div class="p-2 container">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="120px">
            <el-form-item label="设备名称" prop="equipmentName">
              <el-input v-model="queryParams.equipmentName" placeholder="请输入设备名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="设备型号" prop="equipmentName">
              <el-input v-model="queryParams.equipmentName" placeholder="请输入设备型号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="设备编码" prop="equipmentCode">
              <el-input v-model="queryParams.equipmentCode" placeholder="请输入设备编码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="设备类型" prop="equipmentTypeName">
              <el-input v-model="queryParams.equipmentTypeName" placeholder="请输入设备类型" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="设备识别码" prop="nodeId">
              <el-input v-model="queryParams.nodeId" placeholder="请输入设备识别码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="录入时间" prop="updateTime">
              <el-date-picker
                v-model="queryParams.updateTime"
                clearable
                style="width: 200px;"
                type="date"
                placeholder="请选择"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never" style="flex: 1; display: flex; flex-direction: column; position: relative;">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['camera:camera-info:add']" type="primary" plain icon="Plus" @click="openAdd">新增</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="quipmentList" style="flex: 1;" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="设备名称" align="center" prop="equipmentName" :min-width="100" show-overflow-tooltip />
        <el-table-column label="设备编码" align="center" prop="equipmentCode" :min-width="160" show-overflow-tooltip />
        <el-table-column label="设备识别码" align="center" prop="deviceId" min-width="160px" show-overflow-tooltip />
        <el-table-column label="设备一级类型" align="center" prop="equipmentTypeLevel1Name" min-width="120px" show-overflow-tooltip />
        <el-table-column label="设备二级类型" align="center" prop="equipmentTypeLevel2Name" min-width="120px" show-overflow-tooltip />
        <el-table-column label="设备分组" align="center" prop="groupName" :min-width="100" show-overflow-tooltip />
        <el-table-column label="安装位置" align="center" prop="position" :min-width="160" show-overflow-tooltip>
          <template #default="scope">
            {{ getParkPositionStr(scope.row.parkBuildingName, scope.row.parkBuildingAreaName, scope.row.parkBuildingFloorName, scope.row.parkBuildingRoomName) }}
          </template>
        </el-table-column>
        <el-table-column label="设备状态" align="center" prop="status">
          <template #default="scope">
            <el-switch
              v-model="switchOpen"
              inline-prompt
              active-text="在线"
              inactive-text="停用"
              @change="SwitchOpen(scope.row, scope.$index)"
            />
          </template>
        </el-table-column>
        <el-table-column label="运行状态" align="center" prop="onlineStatus">
          <template #default="scope">
            <div v-if="switchOpen">
              <span class="ldian">●</span>
              <span>在线</span>
            </div>
            <div v-else>
              <span class="hdian">●</span>
              <span>离线</span>
            </div>
            <div v-else>
              <span class="gzdian">●</span>
              <span>故障</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="录入时间" align="center" prop="updateTime" :min-width="170" show-overflow-tooltip >
          <template #default="scope">
            <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}  {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" :min-width="150" class-name="small-padding fixed-width" >
          <template #default="scope">
            <el-tooltip content="详情" placement="top">
              <el-button v-hasPermi="['fire:controlEquipment:edit']" link type="primary" @click="openDetails(scope.row)">详情</el-button>
            </el-tooltip>
            <el-tooltip content="修改" placement="top">
              <el-button v-hasPermi="['fire:controlEquipment:edit']" link type="primary" @click="openUpdate(scope.row)">修改</el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button v-hasPermi="['fire:controlEquipment:remove']" link type="primary" @click="handleDelete(scope.row)">删除</el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>

    <!-- 新增 -->
    <el-drawer v-model="dialog.showAdd" :title="dialog.title" size="800px" append-to-body destroy-on-close >
      <add @close="dialog.showAdd = false" @success="getList()"></add>
    </el-drawer>

    <!-- 修改消防设备对话框 -->
    <el-drawer v-model="dialogEdit.shouAdd" :title="dialogEdit.title" size="800px" append-to-body destroy-on-close >
      <update :equipment-id="fireControlEquipmentId" @close="dialogEdit.shouAdd = false" @success="getList()"></update>
    </el-drawer>

    <!-- 详情 消防设备对话框 -->
    <el-drawer v-model="dialog.details" :title="dialogEdit.title" size="950px" append-to-body destroy-on-close >
        <detail :fire-control-equipment-id="fireControlEquipmentId" @close="dialog.details = false"></detail>
    </el-drawer>
  </div>
</template>

<script setup name="controlequipment" lang="ts">
import { controlequipmentVO } from '@/api/security/firefighting/fire-equipment/types';
import { listcamera } from '@/api/security/firefighting/fire-equipment-group';
import add from './components/add.vue'
import detail from './components/detail.vue';
import update from './components/update.vue';
import request from '@/utils/request';
import { getParkPositionStr } from '@/utils/parkUtil';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const quipmentList = ref<controlequipmentVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const total = ref(0);
const queryFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const dialogEdit = reactive<DialogOption>({
  visible: false,
  title: ''
});

let switchOpen = ref(true)

const queryParams = ref <any>({
  pageNum: 1,
  pageSize: 10,
  equipmentName: undefined,
  equipmentCode: undefined,
  equipmentTypeLevel1Id: 4,
  params: { }
});

/** 查询消防设备列表 */
const getList = async () => {
  loading.value = true;
  const res = await request({ url: '/equipment/equipment-info/list', method: 'get', params: queryParams.value }).finally(() => (loading.value = false))
  quipmentList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: controlequipmentVO[]) => {
  ids.value = selection.map(item => item.fireControlEquipmentId);
}

/** 开关change事件 */
const SwitchOpen = (row, index) => {
  // switchOpen.value = !switchOpen.value
}

/** 查询摄像头信息列表 */
const openAdd = async () => {
  dialog.showAdd = true;
  dialog.title = '添加消防设备';
}

/** 修改按钮操作 */
const fireControlEquipmentId = ref()
const openUpdate = async (row) => {
  fireControlEquipmentId.value = row?.equipmentId || ids.value[0]
  dialogEdit.shouAdd = true;
  dialogEdit.title = '修改消防设备';
}
/** 详情按钮操作 */
const openDetails = async (row?: controlequipmentVO) => {
  fireControlEquipmentId.value = row?.equipmentId || ids.value[0]
  dialog.details = true;
  dialogEdit.title = '详情';
}

/** 删除按钮操作 */
const handleDelete = async (row?: controlequipmentVO) => {
  const _accessControlInfoIds = row?.equipmentId || ids.value;
  await proxy?.$modal.confirm('是否确认删除门禁信息编号为"' + _accessControlInfoIds + '"的数据项？').finally(() => (loading.value = false));
  await request({ url: '/equipment/equipment-info/remove-equipment-type/' + _accessControlInfoIds, method: 'delete' })
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 摄像头下拉 */
let optionsCameraList = ref([])
const initCameraEquipList = async() =>{
  const res = await listcamera()
  optionsCameraList.value = res
}

onMounted(() => {
  getList();
  initCameraEquipList()
});
</script>
<style lang="scss" scoped>
:deep(.el-card__header) {
  height: 60px;
}
:deep(.el-card__body) {
  height: calc(100% - 60px);
  display: flex;
  flex-direction: column;
}
:deep(.pagination-container) {
  margin-top: 0;
}

.ldian {
  margin-right: 10px;
  font-weight: 400;
  color: #51D351;
}
.hdian {
  margin-right: 10px;
  font-weight: 400;
  color: #FF9900;
}
.gzdian {
  margin-right: 10px;
  font-weight: 400;
  color: #FB6260;
}
</style>
